package Model;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;

public class MonteCarloSimulation {
	
	protected ArrayList<Option> options;
	protected BufferedReader reader;
	
	public MonteCarloSimulation(){
		options = new ArrayList<Option>();
		getData();
	}
	
	public double standartOption( String CallPutFlag, double S, double X, double T, double r, double b, double v, int nSteps, int nSimulations){
			double dt, St;
			double Sum = 0, Drift, vSqrdt;
			int i, j, z = 0;
			Random rand = new Random();
			
			dt = T / nSteps;
			
			Drift = (b - Math.pow(v,2) / 2) * dt;
			
			vSqrdt = v * Math.sqrt(dt);
			if (CallPutFlag=="call")
			  z=1;
			else if (CallPutFlag=="put")
			  z = -1;
			  
			for(i = 1; i<=nSimulations; i++){
			   St = S;
			   for(j = 1; j<=nSteps;j++){
			    	St = St * Math.exp(Drift + vSqrdt * rand.nextGaussian());
			   }
			   Sum = Sum + Math.max(z * (St - X), 0);
			}
			return Math.exp(-r * T) * (Sum / nSimulations); 
	}
	
	public void getData() {
		try {
			reader = new BufferedReader(new FileReader("data"));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String info = null;
		
		try {
			while((info = reader.readLine()) != null) {
				String[] infoData = info.split(",");
				Option option = new Option(null,Double.parseDouble(infoData[0]),Double.parseDouble(infoData[1]),Double.parseDouble(infoData[2]),Double.parseDouble(infoData[3]),Double.parseDouble(infoData[4]),Double.parseDouble(infoData[5]),Integer.parseInt(infoData[6]),Integer.parseInt(infoData[7]));
				options.add(option);
		    }
		} catch (NumberFormatException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public ArrayList<Option> getOptions() {
		return options;
	}

	public void setOptions(ArrayList<Option> options) {
		this.options = options;
	}
}
